package com.shushan.mixedexam.englishwords.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shushan.shushanbao.R;
import com.shushan.util.DensityUtil;

import java.util.List;


/**
 * @ClassName: WordCharsGvAdapter
 * @Desciption: //TODO
 * @author: zhangshihao
 * @date: 2018-07-27
 */
public class WordCharsGvAdapter extends BaseAdapter {

    private static final String TAG = WordCharsGvAdapter.class.getSimpleName();

    private Context context;
    private List<Character> wordChars;

    public WordCharsGvAdapter(Context context, List<Character> chars){
        this.context = context;
        this.wordChars = chars;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_word_chars,null,false);
            holder = new ViewHolder();
            holder.rlContainer = convertView.findViewById(R.id.rl_container);
            holder.tvChar = convertView.findViewById(R.id.tv_strange_word);
            holder.ivDelete = convertView.findViewById(R.id.iv_delete);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        int screenWidth = DensityUtil.getScreenWidth(context);
        int boxWidth = (screenWidth - DensityUtil.dip2px(context,60))/5;
        Log.i(TAG,"boxWidth == "+boxWidth);
        holder.rlContainer.setLayoutParams(new RelativeLayout.LayoutParams(boxWidth,boxWidth));
        if(String.valueOf(wordChars.get(position)).equals("_")){
            holder.tvChar.setVisibility(View.GONE);
            holder.ivDelete.setVisibility(View.VISIBLE);
        }else{
            holder.tvChar.setText(String.valueOf(wordChars.get(position)));
            holder.ivDelete.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public int getCount() {
        return wordChars.size();
    }

    @Override
    public Object getItem(int position) {
        return wordChars.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder{
        private RelativeLayout rlContainer;
        private TextView tvChar;
        private ImageView ivDelete;
    }

}
